﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web;
using System.IO;
using System.Text;
using Common;

using CSharpConnectMySQL;
using System.Data;

/*
 * 打标机
 */

namespace DingApi.Controllers
{
    [RoutePrefix("api/Heat_MatNos")]
    public class Heat_MatNosController : ApiController
    {

        string ps_name = "Heat_MatNos";

        int i = get_order2.get_order_num();



        [HttpPost]
        [Route("PostGetHeat_MatNos")]
        public System.Data.DataSet PostGetHeat_MatNos([FromBody]  JObject obj)
        {
            string ip = ClassMyFile.get_ip();


            ClassMyFile.WriteLog("get mat_no  begin----ip["+ ip + "]----------------------------  ", ps_name);

           

            DateTime dt0 = DateTime.Now;
            DateTime dt1 = DateTime.Now;
            TimeSpan timeDifference;
            int subTime = 0;


            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables[0].TableName = "SYS";
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");

            if (ip != "192.168.121.231"                 
                && ip != "192.168.121.250"
                && ip != "192.168.121.89"
                )
            {
                ClassMyFile.WriteLog("ip[" + ip + "]非法操作！", ps_name);

                outBlock.Tables["SYS"].Rows.Add(-1, "ip[" + ip + "]非法操作！");
                return outBlock;
            }




            //逻辑校验；
            ClassMyFile.WriteLog("本日调用次数[" + i.ToString() + "],上次调用时间=[" + get_order2.get_dt_last().ToString("yyyy-MM-dd HH:mm:ss") + "]", ps_name);
             

            timeDifference = DateTime.Now - get_order2.get_dt_last();
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
            
            if (subTime <= 3000)
            {
                ClassMyFile.WriteLog("end---错误：请求间隔不得少于3秒", ps_name);

                outBlock.Tables["SYS"].Rows.Add(-1, "请求间隔不得少于3秒！");
                return outBlock;
            }






            string sql = "";
            try
            {
                 sql = "   SELECT      "
                    + "     t.serial_number,t.heat_number,t.erp_heat_number,t.sg_number,t.is_hot   "
                    + "     ,f.plan_number,f.sg_sign,f.sg_std,f.plan_length "
                    + "     FROM    t_material t, t_input_plan f "  
                    + "     WHERE t.flow_id = 'jiaReLu'   "
                    + "     and t.sta_code = 'processing'   "
                    + "     and t.input_plan_id = f.id      " 
                    + "     ORDER BY     t.start_flow_time DESC "
                    ;
                 


                DataSet dataSet = MySqlHelper2.GetDataSet(sql);
                if (dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count == 0)
                {
                    ClassMyFile.WriteLog("end---无数据sql\n："+ sql + "", ps_name);
                    outBlock.Tables["SYS"].Rows.Add(-1, "无数据");
                    return outBlock;
                }
                else
                {
                    outBlock.Tables["SYS"].Rows.Add(1, "成功");
                    outBlock.Tables.Add(dataSet.Tables[0].Copy());
                }
            }
            catch (Exception ex)
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "异常错误["+ex.ToString()+"]");

                ClassMyFile.WriteLog(string.Format("end----- -------异常错误{0}--\n{1}   ", ex.ToString(), sql), ps_name);

            }  

            timeDifference = DateTime.Now - dt0;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
             

            ClassMyFile.WriteLog(string.Format("end--------------------------------总耗时{0}ms  ", subTime), ps_name); 
            get_order2.set_dt_last(); 
            return outBlock;

        }


 

       










    }





}